//处理哦登录，注册，找回密码等操作
const jwt = require("jsonwebtoken");
module.exports = class Login extends require('./model') {
  constructor() {
    super()
  }
  //手机号+密码登录
  static pwdLogin(telephone, password) {
    const sql = 'select id,username,telephone,avatarUrl,token from user where telephone=? and password = ?';
    return this.query(sql, [telephone, password]);
  }
  //短信验证码登录
  static verifyCodeLogin(telephone) {
    const sql = 'select id,username,telephone,avatarUrl,token from user where telephone=?';
    return this.query(sql, [telephone]);
  }
  //查询用户是否存在
  static isExists(telephone) {
    const sql = `select * from user where telephone=?`;
    return this.query(sql, [telephone]);
  }
  //注册用户
  static registerUser(username, password, telephone) {
    //默认头像
    const avatarUrl = 'https://img0.baidu.com/it/u=4925141,1914114694&fm=26&fmt=auto&gp=0.jpg';
    //生成token
    const token = jwt.sign(telephone + username + Date.now() + Math.random() * 100000000000000000,'secretOrPrivateKey');
    const sql = `insert into user (username,password,telephone,avatarUrl,token) values (?,?,?,?,?);`;
    return this.query(sql, [username, password, telephone, avatarUrl, token]);
  }
  //修改用户密码
  static changePassword(telephone, password) {
    const sql = `update user set password = ? where telephone = ?`;
    return this.query(sql, [password, telephone]);
  }
}